#!/usr/bin/python

import os
import cgi
from nspot import SystemMessages, Arp, SystemProperties, UserDAO, BlockedDAO, MessageDAO, BannedDAO, UserHandler, FirewallInitializer, BandwidthControl
import cgitb; cgitb.enable()
import datetime
from Cookie import SimpleCookie
from Cheetah.Template import Template

form = cgi.FieldStorage()

arp = Arp()
sp = SystemProperties()
ud = UserDAO()
sm = SystemMessages()
ip = os.environ['REMOTE_ADDR']

t = Template(file = sp.get_web_root()+'/htdocs/pages/templates/baselayout.html')

t.system_message = 'System Administration' #self.sm.msg_allfields()
t.msg_login = '' #sm.msg_login()
t.page_title = sm.get_title()
t.head_redirect = "no" # can be yes or no
t.head_to_login = "no" # can be yes or no
t.body_page = "adm" # can be login, register or none
t.body_message = ''


t.users = UserDAO().retrieve_list()

mac = arp.get_mac_from_arp(ip)
user = ud.retrieve_by_mac(mac)
bld = BlockedDAO()
bnd = BannedDAO()

t.mac = mac
t.user = user
t.bld = bld
t.bnd = bnd

cmd = form.getvalue('cmd')
uid = form.getvalue('uid')
msg = form.getvalue('msg')
dst = form.getvalue('dst')

t.cmd = cmd
t.uid = uid
t.msg = msg
t.dst = dst

us = UserDAO().retrieve_by_id(uid)
users = UserDAO().retrieve_list()
uh = UserHandler(us)

if os.environ.has_key('HTTP_COOKIE'):
    c = SimpleCookie(os.environ['HTTP_COOKIE'])
else:
    c = SimpleCookie()

if not c.has_key('adm'):
    c['adm'] = '0'

print c


print "Content-type: text/html\n\n"

if c['adm'].value == '1':
    if cmd == 'blk':
	uh.block_user()
    elif cmd == 'ubk':
	uh.unblock_user()
    elif cmd == 'ban':
	uh.ban_user()
    elif cmd == 'ubn':
	uh.unban_user()
    elif cmd == 'bdp':
	ud.update_band(uid, int(us.bandwidth) + 64)
	uh.set_band()
    elif cmd == 'bdm':
	ud.update_band(uid, int(us.bandwidth) - 64)
	uh.set_band()
    elif cmd == 'del':
	uh.deny()
	uh.unban_user()
	uh.unblock_user()
	ud.delete(uid)
    elif cmd == 'mpg':
	t.body_page = "msg"
    elif cmd == 'rst':
	FirewallInitializer().stop()
	FirewallInitializer().start()
	BandwidthControl().stop()
	BandwidthControl().start()
    elif cmd == 'msg':
	if dst == str(uid):
	    MessageDAO().create(us,msg)
	elif dst == 'all':
	    for i in users:
		MessageDAO().create(i,msg)
	    
    print t
	    
else:
    print '<meta http-equiv="refresh" content="0; URL=http://'+sp.get_ip_internal()+'/cgi-bin/auth">'
